package com.qf.j2205.controller;

import com.alibaba.fastjson.JSON;
import com.qf.j2205.entity.VipUser;
import com.qf.j2205.service.VipUserService;
import com.qf.j2205.service.impl.VipUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName VipUserServlet
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/17 9:52
 * @Version 1.0
 **/
public class VipUserServlet extends ServletBase{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    /**
     * 添加vipuser
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("txtUName");
        String vcid = req.getParameter("txtUidentity");
        String tele = req.getParameter("txtUTel");
        String fruit = req.getParameter("Fruit").equals("0")?"个人用户":"团体用户";
        String team = req.getParameter("txtUteamname");
        String pwd = req.getParameter("txtUPWD");
        VipUser vipUser = new VipUser(0, name, pwd, vcid, tele, fruit, team);
        VipUserService service = new VipUserServiceImpl();
        boolean b = service.insertVipUser(vipUser);
        if (b){
            resp.sendRedirect("/vipUser/OrderUserInfo.html");
        }else {
            resp.sendRedirect("/error.html");
        }
    }

    /**
     * 查询全部数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        VipUserService service = new VipUserServiceImpl();
        List<VipUser> all = service.findAll();
        if (all.size() > 0){
            String json = JSON.toJSONString(all);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.write(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int vid = Integer.parseInt(req.getParameter("vid"));
        VipUserService service = new VipUserServiceImpl();
        boolean b = service.deleteVip(vid);
        if (b){
            String json = JSON.toJSONString("success");
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.write(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }

    /**
     * 条件查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void conditionFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("vname").isEmpty()?null: req.getParameter("vname");
        String vcid = req.getParameter("vcid").isEmpty()?null : req.getParameter("vcid");
        int i = Integer.parseInt(req.getParameter("type").isEmpty() ? "0" : req.getParameter("type"));
        String[] string={null,"个人用户","团体用户"};
        VipUserService service = new VipUserServiceImpl();
        List<VipUser> users = service.conditionFind(name, vcid, string[i]);
        if (users.size() > 0){
            String json = JSON.toJSONString(users);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.write(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }

    /**
     * 根据id查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findOneById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int vid =Integer.parseInt(req.getParameter("vid").isEmpty()?null : req.getParameter("vid"));

//        System.out.println(vid);
        VipUserService service = new VipUserServiceImpl();

        VipUser users = service.findOneById(vid);
        if (users!=null){
            String json = JSON.toJSONString(users);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.write(json);
            writer.flush();
            writer.close();
        }else {
            resp.sendRedirect("error.html");
        }
    }


    protected void editVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int vid = Integer.parseInt(req.getParameter("vid"));
        String name = req.getParameter("txtUName");
        String vcid = req.getParameter("txtUidentity");
        String tele = req.getParameter("txtUTel");
        String fruit = req.getParameter("Fruit");
        String team = req.getParameter("txtUteamname");
        if (fruit.equals("个人用户")){
            team = "";
        }
        String pwd = req.getParameter("txtUPWD");
        VipUser vipUser = new VipUser(vid, name, pwd, vcid, tele, fruit, team);
        VipUserService service = new VipUserServiceImpl();
        boolean b = service.editVip(vipUser);
        if (b){
            resp.sendRedirect("/vipUser/OrderUserInfo.html");
        }else {
            resp.sendRedirect("/error.html");
        }
    }
}
